﻿using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using XamlingCore.SL.AnimationChainer;

namespace XamlingCore.SL.Controls.Zazz
{
    public partial class ItsSnowing : UserControl
    {

        private int fCount = 100;

        Random randNo;

        public ItsSnowing()
        {
            InitializeComponent();
            byte[] rand = new byte[100];
            RNGCryptoServiceProvider randomProc = new RNGCryptoServiceProvider();
            randomProc.GetBytes(rand);

            randNo = new Random(BitConverter.ToInt32(rand, 0));
            this.Loaded += new RoutedEventHandler(ItsSnowing_Loaded);
        }

        void ItsSnowing_Loaded(object sender, RoutedEventArgs e)
        {
            loadFliers();
        }

        void loadFliers()
        {

            Fliers.Children.Clear();

            double maxWidth = this.ActualWidth;
            double maxHeight = this.ActualHeight;

            for (int i = 0; i < fCount; i++)
            {
                //create the random ellipses
                int wh = randNo.Next(2, 5);

                Ellipse e = new Ellipse();
                e.Width = wh;
                e.Height = wh;

                e.SetValue(Canvas.TopProperty, (double)randNo.Next(0, Convert.ToInt32(maxHeight)));
                e.SetValue(Canvas.LeftProperty, (double)randNo.Next(0, Convert.ToInt32(maxWidth)));

                e.Fill = new SolidColorBrush(Colors.White);

                //line up the first set of animations



                Fliers.Children.Add(e);

                reDoAnims(null, e);
            }
        }

        void reDoAnims(AnimationConfig g, object context)
        {
            AnimationChainManager m = new AnimationChainManager(this);

            // the main guts of the animation... each time one animation finishes, this code is called by the callback.
            Ellipse tt = context as Ellipse;

            double maxWidth = this.ActualWidth;
            double maxHeight = this.ActualHeight;

            double moveTop = (double)randNo.Next(0, Convert.ToInt32(maxHeight));
            double moveLeft = (double)randNo.Next(0, Convert.ToInt32(maxWidth));

            int keyTime = randNo.Next(8, 20);

            Debug.WriteLine(DateTime.Now.ToString() + "Redo");

            tt.Opacity = 0;

            m.Add()
                   .DoubleAnimationK()
                   .Target(tt)
                   .Property(Canvas.LeftProperty)
                   .KeyFrame(moveLeft, new TimeSpan(0, 0, keyTime))

                   .Queue()

                   .DoubleAnimationK()
                   .Property(Canvas.TopProperty)
                   .KeyFrame(moveTop, new TimeSpan(0, 0, keyTime))
                   .Queue()

                   .CompleteAction((s, e) => { reDoAnims(null, tt); })

                   .DoubleAnimationK()
                   .Property(Ellipse.OpacityProperty)

                   .KeyFrame(0, TimeSpan.Zero)
                   .KeyFrame(1, TimeSpan.FromMilliseconds(3000))
                   .KeyFrame(1, new TimeSpan(0, 0, keyTime - 6))
                   .KeyFrame(0, new TimeSpan(0, 0, 3))

                   .Queue();

            m.Begin(TimeSpan.Zero);
        }
    }
}
